package com.luxiu.extensionlib.imageView

import android.content.Context
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import com.luxiu.extensionlib.R

class RoundCornerImageView(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : androidx.appcompat.widget.AppCompatImageView(context, attrs, defStyleAttr) {

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0)

    init {
        val ta = context.obtainStyledAttributes(attrs, R.styleable.RoundCornerImageView, defStyleAttr, 0)
        try {
            // 创建一个 GradientDrawable 作为背景
            val drawable = GradientDrawable().apply {
                shape = GradientDrawable.RECTANGLE
                cornerRadius = ta.getDimension(R.styleable.RoundCornerImageView_mCardImageCornerRadius, 0f)
            }
            background = drawable
            clipToOutline = true
        } finally {
            ta.recycle()
        }
    }
}